﻿using DevExpress.XtraEditors.Repository;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using HuaCheng.CommonClass;
using HuaCheng.Controller;
using HuaCheng.DataAccess;
using HuaCheng.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace HuaCheng.Views
{
    /// <summary>
    /// 标准通道信息页
    /// </summary>
    public partial class ChannelInfo : Form
    {
        #region 全局变量
        //定义单元格模板
        RepositoryItem TextNull = new RepositoryItemTextEdit();
        //定义工步方案集合
        public Dictionary<int, List<StepInfo>> stepchemeList = new Dictionary<int, List<StepInfo>>();
        //定义当前通道工步集合
        //List<StepInfo> currentList = new List<StepInfo>();
        //定义测试信息对象
        Test test = new Test();
        //从表中读取工步数据
        List<byte[]> byteList;
        //定义单位对象
        public UnitOrder uOrder;
        //电压量程
        public string volRange;
        //电流量程
        public string curRange;
        //当前页码 默认为0
        public int currentIndex;
        //测试号
        public string testid;
        //设备号
        public string ip;
        //柜号
        public int gid;
        //上位机号
        public int mid;
        //通道号
        public int cid;
        //总页码
        public static int totalIndex;
        #endregion
        public ChannelInfo(string testId,string ip,int gid,int mid,int cid)
        {
            InitializeComponent();
            this.testid = testId;
            this.ip = ip;
            this.gid = gid;
            this.cid = cid;
            this.mid = mid;
            InitUnit();//初始化单位
            InitParams(testId,ip, gid,mid, cid);//初始化获得表的数据源          
            LoadStepInfo(0);//把数据源赋给下面的表控件          
            InitFirstTable(testId,ip, gid, mid,cid,0);//把数据源赋给上面的表控件
        }
        /// <summary>
        /// 当前页属性
        /// </summary>
        public int CurrentIndex 
        { 
            get { return currentIndex; }
            set { SetBtnEnable(ref currentIndex, value); }
        }
        /// <summary>
        /// 设置页面时监听页码变化 来切换按钮的属性
        /// </summary>
        /// <param name="currentIndex"></param>
        /// <param name="value"></param>
        private void SetBtnEnable(ref int currentIndex, int value)
        {
            currentIndex = value;
            if (currentIndex > 0)
            {
                this.btnFrontSche.Enabled = true;
            }
            else
            {
                this.btnFrontSche.Enabled = false;
            }
            if (currentIndex >= totalIndex-1)
            {
                this.btnNextSche.Enabled = false;
            }
            else
            {
                this.btnNextSche.Enabled = true;
            }
        }
        #region 通道信息页面初始化
        /// <summary>
        /// 从配置文件读取单位
        /// </summary>
        public void InitUnit()
        {
            uOrder = UnitOrderControl.GetUnit();
            this.tLStepTime.Caption = ResourceCulture.GetString("StepTime") + "(" + uOrder.StepEditTime + ")";
            this.tLVoltage.Caption = ResourceCulture.GetString("lblVoltageText") + "(" + uOrder.Voltage + ")";
            this.tLCurrent.Caption = ResourceCulture.GetString("lblCurrentText") + "(" + uOrder.Current + ")";
            this.tLCap.Caption = ResourceCulture.GetString("Chg_Cap") + "(" + uOrder.Capacity + ")";
            this.tLEnergy.Caption = ResourceCulture.GetString("Chg_Eng") + "(" + uOrder.Energy + ")";
            this.tLDeltaVolt.Caption = "-ΔV(" + uOrder.Voltage+ ")";
            this.tLPower.Caption = ResourceCulture.GetString("Power") + "(" + uOrder.Power + ")";
            this.tLLoadResistor.Caption = ResourceCulture.GetString("Resistance") + "(" + uOrder.Resistance + ")";
            this.tLCutoffCurr.Caption = ResourceCulture.GetString("EndCurrent") + "(" + uOrder.Current + ")";
            this.tLMaxVi.Caption = "MaxVi(" + uOrder.Voltage + ")";
            this.tLMinVi.Caption = "MinVi(" + uOrder.Voltage + ")";
            this.tLMaxTi.Caption = "MaxTi(" + uOrder.Template + ")";
            this.tLMinTi.Caption = "MinTi(" + uOrder.Template + ")";
            this.tLSingleVolt.Caption = ResourceCulture.GetString("SinglecellVoltageStopCondition") + "(" + uOrder.Voltage + ")";
        }
        /// <summary>
        /// 读取工步表中数据和测试表数据
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="gid"></param>
        /// <param name="cid"></param>
        public void InitParams(string testId,string ip, int gid,int mid, int cid)
        {
            try //从表中读取工步数据
            {                
                //从工步方案表中查询某通道一次测试中所有工步方案
                byteList = StepSchemeHelper.RetriveStepScheme(testId, ip, gid,mid ,cid);
                totalIndex = byteList.Count;//获取当前测试的总页码
                if (byteList.Count > 0)
                {
                    List<StepInfo> currentList = new List<StepInfo>();
                    for (int i = 0; i < byteList.Count; i++)
                    {                       
                        //反序列化每个工步方案的工步信息
                        List<StepInfo> stepInfo = (List<StepInfo>)SerializableControl.DeserializeWithBinary(byteList[i]);
                        //将工步list转为当前单位的数值
                        currentList = DataControl.DataConvertToCurrent(stepInfo);
                        //添加到工步方案字典中
                        stepchemeList.Add(i, currentList);
                    }
                    if (byteList.Count > 1)//当有超过一个工步方案时
                    {
                        this.btnFrontSche.Visible = true;
                        this.btnNextSche.Visible = true;
                        this.btnNextSche.Enabled = true;
                        this.btnFrontSche.Enabled = false;
                    }
                }
                volRange = DicHelper.RetrieveVolLower() + "-" + DicHelper.RetrieveVolUpper() + uOrder.Voltage;//电压量程
                curRange = DicHelper.RetrieveCurrLower() + "-" + DicHelper.RetrieveCurrUpper() + uOrder.Current;//电流量程
                test = TestHelper.GetTestInfo(testId,ip, gid, mid,cid);//获取测试表中通道信息
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message + e.StackTrace);
            }    
        }
        public void InitFirstTable(string testId, string ip, int gid,int mid, int cid,int index)
        {            
            this.tLChlInfo.Nodes[0].SetValue(1, "#" + ip + "-" + gid +"-"+ mid +"-" + cid);//设备-单元-通道号
            //如果有工步方案，treeList新增一个节点
            if (stepchemeList.Count>0)
            {
                List<StepInfo> currentList = new List<StepInfo>();
                currentList = stepchemeList[index];//默认显示第一个工步方案
                //记录条件
                string record = currentList[0].ProtCondition.TimeSpan + uOrder.Time + "," + currentList[0].ProtCondition.VoltageSpan + uOrder.Voltage + ","
                    + currentList[0].ProtCondition.CurrentSpan + uOrder.Current;
                //化成变化率
                string volvar = currentList[0].ProtCondition.DetectionTime + uOrder.Time + "," + currentList[0].ProtCondition.VoltageValue + uOrder.Voltage;
                //电压上限
                string volmax = currentList[0].ProtCondition.VoltageMax + uOrder.Voltage;
                //电压下限
                string volmin = currentList[0].ProtCondition.VoltageMin + uOrder.Voltage;
                //工步延时
                string stepdelay = currentList[0].ProtCondition.StepDelay + uOrder.Time;
                //起始工步
                string startnum = Convert.ToString(test.StartStepNum);
                //起始时间
                string starttime = Convert.ToString(test.StartTime);
                //电流下限
                string curmin = currentList[0].ProtCondition.CurrentMin + uOrder.Current;
                //电流上限
                string curmax = currentList[0].ProtCondition.CurrentMax + uOrder.Current;
                //接续延时
                string continuedelay = currentList[0].ProtCondition.ContinueDelay + uOrder.Time;
                //温度上限
                string tempmax = currentList[0].ProtCondition.TemperatureMax + uOrder.Template;
                //电池批号
                string batnum = currentList[0].BatteryNum;
                //创建者
                string creator = currentList[0].Creator;
                //备注
                string txtRemark = currentList[0].Remark;
                //平台电压1
                string platone = currentList[0].ProtCondition.Platform1 + uOrder.Voltage;
                //平台电压2
                string plattwo = currentList[0].ProtCondition.Platform2 + uOrder.Voltage;
                //平台电压3
                string platthree = currentList[0].ProtCondition.Platform3 + uOrder.Voltage;
                //活性物质
                string active = currentList[0].ProtCondition.Activities;

                this.tLChlInfo.Nodes[0].SetValue(3, volmin);//电压下限
                this.tLChlInfo.Nodes[1].SetValue(3, volmax);//电压上限
                this.tLChlInfo.Nodes[1].SetValue(1, starttime);//电压上限
                this.tLChlInfo.Nodes[1].SetValue(5, stepdelay);//工步延时
                this.tLChlInfo.Nodes[2].SetValue(1, startnum);//起始工步
                this.tLChlInfo.Nodes[2].SetValue(3, curmin);//电流下限
                this.tLChlInfo.Nodes[2].SetValue(5, continuedelay);//接续延时
                this.tLChlInfo.Nodes[3].SetValue(3, curmax);//电流上限
                this.tLChlInfo.Nodes[4].SetValue(3, tempmax);//温度上限
                this.tLChlInfo.Nodes[5].SetValue(3, batnum);//电池批号

                this.tLChlInfo.Nodes[6].SetValue(1, record);//记录条件
                //this.tLChlInfo.Nodes[6].SetValue(3, );//电池条码
                this.tLChlInfo.Nodes[7].SetValue(1, volRange);//电压量程
                this.tLChlInfo.Nodes[7].SetValue(3, creator);//创建者
                this.tLChlInfo.Nodes[8].SetValue(3, txtRemark);//备注
                this.tLChlInfo.Nodes[8].SetValue(1, curRange);//电流量程
                this.tLChlInfo.Nodes[9].SetValue(3, platone);//平台电压1
                this.tLChlInfo.Nodes[10].SetValue(3, plattwo);//平台电压2
                this.tLChlInfo.Nodes[11].SetValue(3, platthree);//平台电压3

                this.tLChlInfo.Nodes[12].SetValue(1, volvar);//化成电压变化率
                this.tLChlInfo.Nodes[12].SetValue(3, active);//活性物质
            }
        }
        /// <summary>
        /// 定义单元格属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tLChlInfo_CustomNodeCellEdit(object sender, GetCustomNodeCellEditEventArgs e)
        {
            e.RepositoryItem = TextNull;
        }
        /// <summary>
        /// 根据工步表初始化工步内容
        /// </summary>
        private void LoadStepInfo(int index)
        {
            //工步集合
            List<StepInfo> currentList = new List<StepInfo>();
            //如果有工步方案，treeList新增一个节点
            if (stepchemeList.Count > 0)
            {             
                currentList = stepchemeList[index];//默认显示第一个工步方案
                if (currentList.Count > 0)//有工步信息才赋值
                {
                    tLStepEditor = StepControl.SetTreeListByStepList(currentList, tLStepEditor);//把该通道工步表中的信息单位化以后显示在tLStepEditor
                }              
            }
            if (currentList.Count < 10)//如果不足10行则补上空行
            {
                for (int i = 0; i < 10 - byteList.Count; i++)
                {
                    TreeListNode tmpNode = tLStepEditor.Nodes.Add();
                    tLStepEditor.FocusedNode = tmpNode;
                }
            }
            tLStepEditor.FocusedNode = tLStepEditor.Nodes[0];//聚焦到第一行
        }
        #endregion
        /// <summary>
        /// 上一个工步方案
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFrontSche_Click(object sender, EventArgs e)
        {
            CurrentIndex--;
            //更新第一张表
            InitFirstTable(testid, ip, gid,mid, cid, currentIndex);
            //更新第二张表
            LoadStepInfo(currentIndex);
        }
        /// <summary>
        /// 下一个工步方案
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNextSche_Click(object sender, EventArgs e)
        {
            CurrentIndex++;
            //更新第一张表
            InitFirstTable(testid, ip, gid, mid,cid, currentIndex);
            //更新第二张表
            LoadStepInfo(currentIndex);

        }
        #region 国际化
        private void ChannelInfo_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("ChannelInfo");
            this.tLColChlPropFir.Caption = ResourceCulture.GetString("ChannelProperty");
            this.tLColValFir.Caption = ResourceCulture.GetString("ChannelValue");
            this.tLColChlPropSec.Caption = ResourceCulture.GetString("ChannelProperty");
            this.tLColValSec.Caption = ResourceCulture.GetString("ChannelValue");
            this.tLColChlPropThr.Caption = ResourceCulture.GetString("ChannelProperty");
            this.tLColValThr.Caption = ResourceCulture.GetString("ChannelValue");
            this.tLChlInfo.Nodes[0].SetValue(0, ResourceCulture.GetString("DevUnitChl"));//设备_单元_通道号
            this.tLChlInfo.Nodes[0].SetValue(2, ResourceCulture.GetString("VolMinText"));//电压下限
            this.tLChlInfo.Nodes[0].SetValue(4, ResourceCulture.GetString("Delayprotectiontime"));//延时时间
            this.tLChlInfo.Nodes[1].SetValue(0, ResourceCulture.GetString("StartTime"));//启动时间
            this.tLChlInfo.Nodes[1].SetValue(2, ResourceCulture.GetString("VolMaxText"));//电压上限
            this.tLChlInfo.Nodes[1].SetValue(4, ResourceCulture.GetString("StepDelay"));//工步延时
            this.tLChlInfo.Nodes[2].SetValue(0, ResourceCulture.GetString("StartStepText"));//起始工步
            this.tLChlInfo.Nodes[2].SetValue(2, ResourceCulture.GetString("CurrMinText"));//电流下限
            this.tLChlInfo.Nodes[2].SetValue(4, ResourceCulture.GetString("ContinueDelay"));//接续延时
            this.tLChlInfo.Nodes[3].SetValue(0, ResourceCulture.GetString("StepTime"));//工步时间
            this.tLChlInfo.Nodes[3].SetValue(2, ResourceCulture.GetString("CurrMaxText"));//电流上限
            this.tLChlInfo.Nodes[3].SetValue(4, ResourceCulture.GetString("ZWJXWJInfo"));//中位机和下位机信息
            this.tLChlInfo.Nodes[4].SetValue(0, ResourceCulture.GetString("StepCap"));//工步容量
            this.tLChlInfo.Nodes[4].SetValue(2, ResourceCulture.GetString("Temp.Upper"));//温度上限
            this.tLChlInfo.Nodes[4].SetValue(4, ResourceCulture.GetString("ServerVersion"));//服务器版本
            this.tLChlInfo.Nodes[5].SetValue(0, ResourceCulture.GetString("xunhuan"));//循环
            this.tLChlInfo.Nodes[5].SetValue(2, ResourceCulture.GetString("PN"));//电池批号
            this.tLChlInfo.Nodes[5].SetValue(4, ResourceCulture.GetString("ClientVersion"));//客户端版本
            this.tLChlInfo.Nodes[6].SetValue(0, ResourceCulture.GetString("RecordConditionText"));//记录条件
            this.tLChlInfo.Nodes[6].SetValue(2, ResourceCulture.GetString("BarcodeText"));//电池条码
            this.tLChlInfo.Nodes[6].SetValue(4, ResourceCulture.GetString("ZWJversion"));//中位机版本
            this.tLChlInfo.Nodes[7].SetValue(0, ResourceCulture.GetString("VoltageMeasuringrange"));//电压量程
            this.tLChlInfo.Nodes[7].SetValue(2, ResourceCulture.GetString("Creator"));//创建者
            this.tLChlInfo.Nodes[7].SetValue(4, ResourceCulture.GetString("SerialnumberoftheZWJ"));//中位机序列号
            this.tLChlInfo.Nodes[8].SetValue(0, ResourceCulture.GetString("CurrentMeasuringrange"));//电流量程
            this.tLChlInfo.Nodes[8].SetValue(2, ResourceCulture.GetString("Remarks"));//备注
            this.tLChlInfo.Nodes[8].SetValue(4, ResourceCulture.GetString("XWJVersionofthemainentrance"));//主通道下位机版本
            this.tLChlInfo.Nodes[9].SetValue(0, ResourceCulture.GetString("StepFile"));//工步文件
            this.tLChlInfo.Nodes[9].SetValue(2, ResourceCulture.GetString("PlatVolt") + "1");//平台电压1
            this.tLChlInfo.Nodes[10].SetValue(0, ResourceCulture.GetString("Channelvalidity"));//通道有效性
            this.tLChlInfo.Nodes[10].SetValue(2, ResourceCulture.GetString("PlatVolt") + "2");//平台电压2
            this.tLChlInfo.Nodes[11].SetValue(0, ResourceCulture.GetString("FormationName"));//化成名称
            this.tLChlInfo.Nodes[11].SetValue(2, ResourceCulture.GetString("PlatVolt") + "3");//平台电压3
            this.tLChlInfo.Nodes[12].SetValue(0, ResourceCulture.GetString("Ratechangeaboutformationvoltage"));//化成电压变化率
            this.tLChlInfo.Nodes[12].SetValue(2, ResourceCulture.GetString("Material"));//活性物质
            this.tLStepNum.Caption = ResourceCulture.GetString("StepID");
            this.tLStepName.Caption = ResourceCulture.GetString("StepName");
            this.tLBeginValue.Caption = ResourceCulture.GetString("StartValue");
            this.tLSlope.Caption = ResourceCulture.GetString("Slope");
            this.tLEndValue.Caption = ResourceCulture.GetString("EndValue");
            this.tLAmplitude1.Caption = ResourceCulture.GetString("Amplitude") + "1";
            this.tLTime1.Caption = ResourceCulture.GetString("Time") + "1";
            this.tLAmplitude2.Caption = ResourceCulture.GetString("Amplitude") + "2";
            this.tLTime2.Caption = ResourceCulture.GetString("Time") + "2";
            this.btnFrontSche.Text = ResourceCulture.GetString("AdvanceProtectParameter");
            this.btnNextSche.Text = ResourceCulture.GetString("NextProtectParameter");
        }
        #endregion
    }
}
